<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增合同')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-tenant-add" autocomplete="off">
            <h4 class="form-header h4">基本信息</h4>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">租户名称：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <select id="lessee" class="form-control" required>
                                <option value="">--请选择租户--</option>
                                <option th:each="lessee:${lessee}" th:value="${lessee.lesseeId}" th:text="${lessee.lesseeName}"></option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">楼栋：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <select id="building" class="form-control" required>
                                <option value="">--请选择楼栋--</option>
                                <option th:each="building:${building}" th:value="${building.buildingId}" th:text="${building.buildingName}"></option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">用途：</label>
                    <div class="col-sm-8">
                        <label class="radio-box"> <input type="radio" name="tenantUse" value="0" checked/> 宿舍 </label>
                        <label class="radio-box"> <input type="radio" name="tenantUse" value="1" /> 个人 </label>
                        <label class="radio-box"> <input type="radio" name="tenantUse" value="2" /> 企业 </label>
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">单元号：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <select id="unit" class="form-control noselect2 selectpicker" data-none-selected-text="请选择单元" multiple>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">面积：</label>
                    <div class="col-sm-8">
                        <input name="tenantArea" id="tenantArea" class="form-control" type="text" required>
                    </div>
                </div>
            </div>

            <div class="col-sm-offset-5">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">合同年限：</label>
                    <div class="input-daterange input-group">
                        <input type="text" name="startTime" onchange="startTimeChange()" class="input-sm form-control" id="datetimepicker-startTime" placeholder="yyyy-MM-dd" required/>
                        <span class="input-group-addon">到</span>
                        <input type="text" name="endTime" onchange="endTimeChange()" class="input-sm form-control" id="datetimepicker-endTime" placeholder="yyyy-MM-dd" required/>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">租户状态：</label>
                    <div class="col-sm-8">
                        <label class="radio-box"> <input type="radio" name="tenantStatus" value="0" checked/> 在租 </label>
<!--                        <label class="radio-box"> <input type="radio" name="tenantStatus" value="1" /> 提前离租 </label>-->
<!--                        <label class="radio-box"> <input type="radio" name="tenantStatus" value="2" /> 正常离租 </label>-->
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">租金起算日：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            <input id="rentStartDay" name="rentStartDay" onchange="rentStartDayChange()" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">每月租金：</label>
                    <div class="col-sm-8">
                        <input name="monthlyRentFee" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label id="risePriceFirstTimeLable" class="col-sm-3 control-label">涨租时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            <input id="risePriceFirstTime" onchange="risePriceFirstTimeChange()" name="risePriceFirstTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label  id="risePriceRatioLabel" class="col-sm-3 control-label">涨租比率：</label>
                    <div class="col-sm-7">
                        <input name="risePriceRatio" id="risePriceRatio" class="form-control" type="text">
                    </div>
                    <div style="float: left"><span style="font-size: 23px">%</span></div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label ">涨租次数：</label>
                    <div class="col-sm-8">
                        <input id="risePriceNumber" name="risePriceNumber" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label id="propertyFeeLabel" class="col-sm-3 control-label">物业费：</label>
                    <div class="col-sm-8">
                        <input id="propertyFee"  name="propertyFee" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                </div>
            </div>

            <h4 class="form-header h4">附加信息</h4>
            <div class="col-sm-offset-6" style="display: none">
                <div class="form-group">
                    <label class="col-sm-3 control-label">涨租状态：</label>
                    <div class="col-sm-8">
                        <label class="radio-box"> <input type="radio" name="risePriceStatus" value="0" checked/> 待涨租 </label>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">押金：</label>
                    <div class="col-sm-8">
                        <input name="pledgeFee" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">装修押金：</label>
                    <div class="col-sm-8">
                        <input name="decorationPledgeFee" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">清理垃圾费：</label>
                    <div class="col-sm-8">
                        <input name="scavengingFee" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-offset-6">
                <div class="form-group">
                    <label class="col-sm-3 control-label">水电周转金：</label>
                    <div class="col-sm-8">
                        <input name="utilityFee" class="form-control" type="text">
                    </div>
                </div>
            </div>

            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">备注：</label>
                    <div class="col-xs-10">
                        <textarea name="remark" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script type="text/javascript">

        var prefix = ctx + "system/tenant"
        $("#form-tenant-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var data = $('#form-tenant-add').serializeArray();
                var buildingId = $.form.selectSelects("building");
                var lesseeId = $.form.selectSelects("lessee");
                var buildingName = $('#building').find("option:selected").text();
                var tenantName = $('#lessee').find("option:selected").text();
                var unitIdStr = $.form.selectSelects("unit");
                var unitNameStr = new Array();
                for(var i = 0;i < unitIdStr.split(",").length; i++){
                    var temp = $('#unit').find("option:selected")[i].innerText;
                    unitNameStr.push(temp);
                }
                data.push({"name": "buildingId", "value": buildingId})
                data.push({"name": "buildingName", "value": buildingName})
                data.push({"name": "unitIdStr", "value": unitIdStr})
                data.push({"name": "unitNameStr", "value": unitNameStr.toString()})
                data.push({"name": "lesseeId", "value": lesseeId})
                data.push({"name": "tenantName", "value": tenantName})
                $.operate.save(prefix + "/add", data);
            }
        }
        //合同开始时间事件触发
        function startTimeChange(){
            var startTime = $("#datetimepicker-startTime").val();
            var endTime = $("#datetimepicker-endTime").val();
            if(startTime!=null && startTime!='' && endTime!=null && endTime!=''){
                if(startTime>endTime){
                    $("#datetimepicker-startTime").val('');
                    $.modal.alert("合同年限有误", modal_status.WARNING);
                    return;
                }
                var risePriceFirstTime = $("#risePriceFirstTime").val();
                if(risePriceFirstTime!=null && risePriceFirstTime!=''){
                    risePriceNumberChange(startTime,endTime,risePriceFirstTime);
                }
            }
        }
        //合同结束时间事件触发
        function endTimeChange(){
            var startTime = $("#datetimepicker-startTime").val();
            var endTime = $("#datetimepicker-endTime").val();
            if(startTime!=null && startTime!='' && endTime!=null && endTime!=''){
                if(startTime>endTime){
                    $("#datetimepicker-endTime").val('');
                    $.modal.alert("合同年限有误", modal_status.WARNING);
                    return;
                }
                var risePriceFirstTime = $("#risePriceFirstTime").val();
                if(risePriceFirstTime!=null && risePriceFirstTime!=''){
                    risePriceNumberChange(startTime,endTime,risePriceFirstTime);
                }
            }
        }
        //租户起租时间事件触发
        function rentStartDayChange(){
            var rentStartDay = $("#rentStartDay").val();
            var startTime = $("#datetimepicker-startTime").val();
            var endTime = $("#datetimepicker-endTime").val();
            if(rentStartDay!=null && rentStartDay!='' && startTime!=null && startTime!='' && endTime!=null && endTime!=''){
                if(rentStartDay>endTime || rentStartDay<startTime){
                    $("#rentStartDay").val('');
                    $.modal.alert("租金起算日不在合同年限内", modal_status.WARNING);
                    return;
                }
            }
        }
        //涨租时间触发
        function risePriceFirstTimeChange(){
            var risePriceFirstTime = $("#risePriceFirstTime").val();
            var startTime = $("#datetimepicker-startTime").val();
            var endTime = $("#datetimepicker-endTime").val();
            if(risePriceFirstTime!=null && risePriceFirstTime!='' && startTime!=null && startTime!='' && endTime!=null && endTime!=''){
                if(risePriceFirstTime>endTime || risePriceFirstTime<startTime){
                    $("#risePriceFirstTime").val('');
                    $.modal.alert("涨租时间不在合同年限内", modal_status.WARNING);
                    return;
                }
                risePriceNumberChange(startTime,endTime,risePriceFirstTime);
            }
        }

        //自动填写租户涨租次数
        function risePriceNumberChange(startTime,endTime,risePriceFirstTime){
            $.ajax({
                url: prefix + "/risePriceNumberChange",    //请求的url地址
                dataType:"json",   //返回格式为json
                async:true,//请求是否异步，默认为异步，这也是ajax重要特性
                data:{"startTime":startTime,"endTime":endTime,"risePriceFirstTime":risePriceFirstTime},    //参数值
                type:"GET",   //请求方式
                success:function(data){
                    //请求成功时处理
                    $("#risePriceNumber").val(data.data);
                },
                error:function(){
                    //请求出错处理
                }
            });
        }

        //楼栋修改触发事件
        $('#building').on('change', function () {
            var buildingId = $.form.selectSelects("building");
            $('#unit').empty();
            // 处理自己的业务
            $.ajax({
                url: prefix + "/addUnitList",    //请求的url地址
                dataType:"json",   //返回格式为json
                async:true,//请求是否异步，默认为异步，这也是ajax重要特性
                data:{"buildingId":buildingId},    //参数值
                type:"POST",   //请求方式
                success:function(data){
                    //请求成功时处理
                    if(data.data!=null){
                        for(var i = 0;i < data.data.length; i++){
                            var unit = data.data[i];
                            $("#unit").append('<option value="'+unit.unitId+'">'+unit.unitName+'</option>');
                        }
                    }
                    else {
                        $("#unit").find('option').remove();
                    }
                    $('#unit').selectpicker('refresh');                   // 刷新
                    $('#unit').selectpicker('render');                   // 渲染
                },
                error:function(){
                    //请求出错处理
                }
            });
        });

        //单元修改触发事件
        $('#unit').on('changed.bs.select', function () {
            var unitIdStrArry = $('#unit').selectpicker('val');
            var unitIdStr = "";
            if (unitIdStrArry != null) {
                unitIdStr = unitIdStrArry.toString();
            }
            $('#tenantArea').empty();
            $.ajax({
                url: prefix + "/addUnitArea",    //请求的url地址
                dataType:"json",   //返回格式为json
                async:true,//请求是否异步，默认为异步，这也是ajax重要特性
                data:{"unitIdStr":unitIdStr},    //参数值
                type:"POST",   //请求方式
                success:function(data){
                    //请求成功时处理
                    if(data!=null){
                        $('#tenantArea').val(data.data);
                    }else {
                        $('#tenantArea').val(0);
                    }
                },
                error:function(){
                    //请求出错处理
                }
            });
        });

        //不同合同类型 切换是否必填
        $('input').on('ifChecked', function(event){
            var tenantUse = $(event.target).val();
            if (tenantUse == "0") {
                $("#risePriceFirstTime").attr("required",false);
                $("#risePriceFirstTimeLable").removeClass("is-required");
            } else if (tenantUse != "0") {
                $("#risePriceFirstTime").attr("required",true);
                $("#risePriceFirstTimeLable").addClass("is-required");
            }
        });
        $('input').on('ifChecked', function(event){
            var tenantUse = $(event.target).val();
            if (tenantUse == "0") {
                $("#risePriceRatio").attr("required",false);
                $("#risePriceRatioLabel").removeClass("is-required");
            } else if (tenantUse != "0") {
                $("#risePriceRatio").attr("required",true);
                $("#risePriceRatioLabel").addClass("is-required");
            }
        });
        $('input').on('ifChecked', function(event){
            var tenantUse = $(event.target).val();
            if (tenantUse == "0") {
                $("#propertyFee").attr("required",false);
                $("#propertyFeeLabel").removeClass("is-required");
            } else if (tenantUse != "0") {
                $("#propertyFee").attr("required",true);
                $("#propertyFeeLabel").addClass("is-required");
            }
        });

        $('#unit').selectpicker({
            'width':'300px',
        });

        $(function() {
            $('#building').select2({
                placeholder: "请选择楼栋",
                allowClear: true
            });
        });

        $(function() {
            $('#lessee').select2({
                placeholder: "请选择租户",
                allowClear: true
            });
        });

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='rentStartDay']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='risePriceFirstTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

    </script>
</body>
</html>